package com.yunchang.springboot.scheduled.mwsreport;

import com.yunchang.springboot.mapper.base.BaseAsinSkuRelationMapper;
import com.yunchang.springboot.mapper.base.BaseSkuInfoMapper;
import com.yunchang.springboot.mapper.base.FinanceShopSkuMapper;
import com.yunchang.springboot.models.base.BaseAsinSkuRelation;
import com.yunchang.springboot.models.base.BaseSkuInfo;
import com.yunchang.springboot.models.mwsreportbean.MwsActiveListingsReport;
import com.yunchang.springboot.models.mwsreportbean.MwsActiveListingsReportDO;
import com.yunchang.springboot.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author czw
 */
@Component
@Slf4j
@Service
public class ReportDateDispose {

    /**
     * asin和sku关联关系表
     */
    private BaseAsinSkuRelationMapper baseAsinSkuRelationMapper;

    @Autowired
    public void setBaseAsinSkuRelationMapper(BaseAsinSkuRelationMapper baseAsinSkuRelationMapper) {
        this.baseAsinSkuRelationMapper = baseAsinSkuRelationMapper;
    }

    /**
     * sku 基础表
     */
    private BaseSkuInfoMapper baseSkuInfoMapper;

    @Autowired
    public void setBaseSkuInfoMapper(BaseSkuInfoMapper baseSkuInfoMapper) {
        this.baseSkuInfoMapper = baseSkuInfoMapper;
    }

    /**
     * 店铺sku关联表
     */
    private FinanceShopSkuMapper financeShopSkuMapper;

    @Autowired
    public void setFinanceShopSkuMapper(FinanceShopSkuMapper financeShopSkuMapper) {
        this.financeShopSkuMapper = financeShopSkuMapper;
    }

    /**
     * @param mwsActiveListingsReportList
     */
    public void skuInfoDispose(List<MwsActiveListingsReportDO> mwsActiveListingsReportList) {
        try {
            if (null != mwsActiveListingsReportList && !mwsActiveListingsReportList.isEmpty()) {
                String today = DateUtil.getTodayDate("yyyy-MM-dd HH:mm:ss");
                String userId = "6666677";
                // sku基础表
                BaseSkuInfo baseSkuInfoInsert = new BaseSkuInfo();
                baseSkuInfoInsert.setParentSku("");
                baseSkuInfoInsert.setAddUser(userId);
                baseSkuInfoInsert.setAddTime(today);
                baseSkuInfoInsert.setModUser(userId);
                baseSkuInfoInsert.setModTime(today);
                baseSkuInfoInsert.setCategory("");
                // asin和sku关联关系
                BaseAsinSkuRelation baseAsinSkuRelation = new BaseAsinSkuRelation();
                baseAsinSkuRelation.setAddUser(userId);
                baseAsinSkuRelation.setAddTime(today);
                baseAsinSkuRelation.setModUser(userId);
                baseAsinSkuRelation.setModTime(today);
                for (MwsActiveListingsReportDO mwsActiveListingsReport : mwsActiveListingsReportList) {
                    String sellerId = mwsActiveListingsReport.getSellerId();
                    String area = mwsActiveListingsReport.getArea();
                    String sku = mwsActiveListingsReport.getSellerSku();
                    String asin1 = mwsActiveListingsReport.getAsin1();
                    if (!StringUtils.isEmpty(sellerId) && !StringUtils.isEmpty(area) && !StringUtils.isEmpty(sku)) {
                        BaseSkuInfo baseSkuInfo = baseSkuInfoMapper.selectBySellerIdAndAreaAndSku(sellerId, area, sku);
                        if (null != baseSkuInfo) {
                            Integer baseSkuInfoIdByNotNull = baseSkuInfo.getId();
                            // 判断sku与asin的关联关系是否存在
                            if (!StringUtils.isEmpty(asin1)) {
                                if (null == baseAsinSkuRelationMapper.selectBySku(baseSkuInfoIdByNotNull, asin1)) {
                                    baseAsinSkuRelation.setAsin(asin1);
                                    baseAsinSkuRelation.setSku(baseSkuInfoIdByNotNull);
                                    // 插入asin和sku关联关系
                                    baseAsinSkuRelationMapper.insertSelective(baseAsinSkuRelation);
                                }
                            }
                            continue;
                        }
                        // 获取品类
//                    String category = financeShopSkuMapper.selectCategoryBySku(sku);
//                    if (StringUtils.isEmpty(category)) {
//                        category = "";
//                    }
                        baseSkuInfoInsert.setSellerId(sellerId);
                        baseSkuInfoInsert.setArea(area);
                        baseSkuInfoInsert.setSku(sku);
                        // 插入sku基础类
                        baseSkuInfoMapper.insertAndReturnId(baseSkuInfoInsert);
                        int baseSkuInfoId =baseSkuInfoInsert.getId();
                        if (!StringUtils.isEmpty(asin1)) {
                            if (null == baseAsinSkuRelationMapper.selectBySku(baseSkuInfoId, asin1)) {
                                baseAsinSkuRelation.setAsin(asin1);
                                baseAsinSkuRelation.setSku(baseSkuInfoId);
                                // 插入asin和sku关联关系
                                baseAsinSkuRelationMapper.insertSelective(baseAsinSkuRelation);
                            }
//                            baseAsinSkuRelation.setAsin(asin1);
//                            baseAsinSkuRelation.setSku(baseSkuInfoId);
//                            // 插入asin和sku关联关系
//                            baseAsinSkuRelationMapper.insertSelective(baseAsinSkuRelation);
                        }
                    }
                }
            }
        } catch (Exception e) {
            log.error("插入数据时出现错误，更新sku基础表及sku与asin的关联关系" + e);
            throw new RuntimeException("插入数据时出现错误，更新sku基础表及sku与asin的关联关系");
        }
    }
}
